#include"SeqList.h"
void SeqListInit(SeqList* ps)
{
  assert(ps);
  ps->arr= NULL;
  ps->capicity= 0;
  ps->size = 0;
}
void SeqListDestroy(SeqList* ps)
{
  assert(ps);
  free(ps->arr);
  ps->arr = NULL;
  ps->capicity = ps->size = 0;
}
                                                                                 
void SeqListPrint(SeqList* ps)
{
  assert(ps);
  for(size_t i = 0;i < ps->size ; ++i)
  {
    printf("%d",ps->arr[i]);
  }
  printf("\n");
}

void CheckCacpity(SeqList *ps)
{
  if(ps->size == ps->capicity)
  {
    size_t newcapacity = ps->capicity == 0 ? 4 : ps->capicity*2;
    ps->arr = (DataType*)realloc(ps->arr,newcapacity * sizeof(DataType));
    ps->capicity = newcapacity;
  }
}
void SeqListPushBack(SeqList* ps, DataType x)
{
  SeqListInsert(ps,ps->size,x);
}
void SeqlistPushFront(SeqList* ps,DataType x)
{
  assert(ps);
  SeqListInsert(ps, 0,x);
}
void SeqListPopFront(SeqList* ps)
{
  assert(ps);
  SeqListErase(ps,0);
}
void SeqListPopBack(SeqList*ps)
{
  assert(ps);

  SeqListErase(ps,ps->size-1);
}
                                                                                 
int SeqListFind(SeqList* ps,DataType x)
{
  for(size_t i = 0; i < ps->size ;++i)
  {
    if(ps->arr[i]==x)
    {
      return i;
    }
  }
  return -1;
}
                                                                                 
void SeqListInsert(SeqList* ps,size_t pos,DataType x)
{
  assert(ps);
  assert(pos <= ps->size);

  CheckCacpity(ps);
  size_t end = ps->size;
  while(end > pos)
  {
    ps->arr[end] = ps->arr[end -1];
    --end;
  }
  ps->arr[pos] = x;
  ps->size++;
}
                                                                           
void SeqListErase(SeqList* ps,size_t pos)
{
   assert(ps);
   assert(pos < ps->size);

   size_t start = pos+1;
   while(start < ps->size)
   {
     ps->arr[start - 1] = ps->arr[start];
     ++start;
   }

   ps->size--;
}
